//
//  WMCropOverlayView.swift
//  deWaterMarkingProject
//
//  Created by admin on 2025/9/11.
//

import UIKit

class WMCropOverlayView: UIView {
    
    var cropRect = CGRect.zero
        var cropCornerRadius: CGFloat = 5.0 // 重命名避免冲突
            
        override init(frame: CGRect) {
            super.init(frame: frame)
            backgroundColor = .clear
        }
        
        required init?(coder: NSCoder) {
            fatalError("init(coder:) has not been implemented")
        }
        
        override func draw(_ rect: CGRect) {
            super.draw(rect)
            
            // Draw semi-transparent overlay
            let overlayPath = UIBezierPath(rect: bounds)
            let cropPath = UIBezierPath(roundedRect: cropRect, cornerRadius: cropCornerRadius) // 使用重命名的属性
            overlayPath.append(cropPath)
            overlayPath.usesEvenOddFillRule = true
            
            UIColor(white: 0.0, alpha: 0.5).setFill()
            overlayPath.fill()
            
            // Draw crop border
            UIColor.white.setStroke()
            cropPath.lineWidth = 2.0
            cropPath.stroke()
            
            // Draw crop corner marks and edge indicators
            drawCornerMarks()
            drawEdgeIndicators()
        }
        
        func drawCornerMarks() {
            let markLength: CGFloat = 20
            let lineWidth: CGFloat = 3.0
            
            UIColor.systemBlue.setStroke()
            
            // Top left
            drawLine(from: CGPoint(x: cropRect.minX, y: cropRect.minY + markLength),
                     to: CGPoint(x: cropRect.minX, y: cropRect.minY))
            drawLine(from: CGPoint(x: cropRect.minX, y: cropRect.minY),
                     to: CGPoint(x: cropRect.minX + markLength, y: cropRect.minY))
            
            // Top right
            drawLine(from: CGPoint(x: cropRect.maxX - markLength, y: cropRect.minY),
                     to: CGPoint(x: cropRect.maxX, y: cropRect.minY))
            drawLine(from: CGPoint(x: cropRect.maxX, y: cropRect.minY),
                     to: CGPoint(x: cropRect.maxX, y: cropRect.minY + markLength))
            
            // Bottom left
            drawLine(from: CGPoint(x: cropRect.minX, y: cropRect.maxY - markLength),
                     to: CGPoint(x: cropRect.minX, y: cropRect.maxY))
            drawLine(from: CGPoint(x: cropRect.minX, y: cropRect.maxY),
                     to: CGPoint(x: cropRect.minX + markLength, y: cropRect.maxY))
            
            // Bottom right
            drawLine(from: CGPoint(x: cropRect.maxX - markLength, y: cropRect.maxY),
                     to: CGPoint(x: cropRect.maxX, y: cropRect.maxY))
            drawLine(from: CGPoint(x: cropRect.maxX, y: cropRect.maxY),
                     to: CGPoint(x: cropRect.maxX, y: cropRect.maxY - markLength))
        }
        
        func drawEdgeIndicators() {
            let indicatorLength: CGFloat = 30
            let lineWidth: CGFloat = 2.0
            
            UIColor.systemBlue.setStroke()
            
            // Top edge
            let topMidX = cropRect.midX
            drawLine(from: CGPoint(x: topMidX - indicatorLength/2, y: cropRect.minY),
                     to: CGPoint(x: topMidX + indicatorLength/2, y: cropRect.minY))
            
            // Bottom edge
            drawLine(from: CGPoint(x: topMidX - indicatorLength/2, y: cropRect.maxY),
                     to: CGPoint(x: topMidX + indicatorLength/2, y: cropRect.maxY))
            
            // Left edge
            let leftMidY = cropRect.midY
            drawLine(from: CGPoint(x: cropRect.minX, y: leftMidY - indicatorLength/2),
                     to: CGPoint(x: cropRect.minX, y: leftMidY + indicatorLength/2))
            
            // Right edge
            drawLine(from: CGPoint(x: cropRect.maxX, y: leftMidY - indicatorLength/2),
                     to: CGPoint(x: cropRect.maxX, y: leftMidY + indicatorLength/2))
        }
        
        func drawLine(from: CGPoint, to: CGPoint) {
            let path = UIBezierPath()
            path.move(to: from)
            path.addLine(to: to)
            path.lineWidth = 3.0
            path.stroke()
        }
        
        // 检测触摸点是否在可拖动区域
        func hitTest(point: CGPoint) -> CropDragType {
            let cornerSize: CGFloat = 30
            let edgeInset: CGFloat = 10
            
            // 检查四个角
            if point.distance(to: cropRect.origin) < cornerSize {
                return .topLeft
            }
            if point.distance(to: CGPoint(x: cropRect.maxX, y: cropRect.minY)) < cornerSize {
                return .topRight
            }
            if point.distance(to: CGPoint(x: cropRect.minX, y: cropRect.maxY)) < cornerSize {
                return .bottomLeft
            }
            if point.distance(to: CGPoint(x: cropRect.maxX, y: cropRect.maxY)) < cornerSize {
                return .bottomRight
            }
            
            // 检查四条边
            if abs(point.x - cropRect.minX) < edgeInset && point.y >= cropRect.minY && point.y <= cropRect.maxY {
                return .left
            }
            if abs(point.x - cropRect.maxX) < edgeInset && point.y >= cropRect.minY && point.y <= cropRect.maxY {
                return .right
            }
            if abs(point.y - cropRect.minY) < edgeInset && point.x >= cropRect.minX && point.x <= cropRect.maxX {
                return .top
            }
            if abs(point.y - cropRect.maxY) < edgeInset && point.x >= cropRect.minX && point.x <= cropRect.maxX {
                return .bottom
            }
            
            // 检查是否在矩形内
            if cropRect.contains(point) {
                return .move
            }
            
            return .none
        }
    }

    // CGPoint扩展，计算两点之间的距离
    extension CGPoint {
        func distance(to point: CGPoint) -> CGFloat {
            return sqrt(pow(x - point.x, 2) + pow(y - point.y, 2))
        }
    }

    // 拖动类型枚举
    enum CropDragType {
        case none
        case move
        case topLeft, topRight, bottomLeft, bottomRight
        case top, bottom, left, right
    }
